package com.bw.shopping.mvvm.view;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm.mvvm.view.BaseActivity;
import com.bw.shopping.BR;
import com.bw.shopping.R;
import com.bw.shopping.databinding.ActivitySeckillParticularsBinding;
import com.bw.shopping.mvvm.model.entity.SeckillAction;
import com.bw.shopping.mvvm.viewmodel.SeckillParticulaesViewModel;
import com.bw.shopping.time.TimeService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;


public class SeckillParticulaesActivity extends BaseActivity<ActivitySeckillParticularsBinding, SeckillParticulaesViewModel> {

    public TimeService service;
    public TextView time;

    public boolean serviceFlag = false;
    @Override
    public int bindLayout() {
        return R.layout.activity_seckill_particulars;
    }

    @Override
    public int initVerable() {
        return BR.particulaes;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        time = findViewById(R.id.time_seckill);

    }

    @Subscribe(sticky = true)
    public void skill(SeckillAction action){
        if (action.action.equals("ScekillValue")){
            LogUtils.e("ScekillValue");
            serviceFlag = true;
            Intent intent = new Intent(this, TimeService.class);
            intent.putExtra("time",System.currentTimeMillis()/1000);
            startService(intent);
            bindService(intent,connection,BIND_ABOVE_CLIENT);
        } else if (action.action.equals("ScekillNowTime")) {
            long time = service.getTime();
            mViewModel.requestScekill(time);
        }

    }
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            TimeService.TimeBinder binder = (TimeService.TimeBinder) iBinder;
            service = binder.getService();
            service.setGetTimesss(new TimeService.GetTime() {
                @Override
                public void myTime(long myTime) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    long mytime = myTime*1000;
                    String dateString = formatter.format(mytime);
                    time.setText("当前时间："+dateString);
                }
            });
        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (serviceFlag){
            Intent intent = new Intent(this, TimeService.class);
            bindService(intent,connection,BIND_ABOVE_CLIENT);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (serviceFlag){
            unbindService(connection);
        }
    }
}
